package com.kaizeli.website.controller;

import com.kaizeli.website.pojo.vo.ProjectMemberCardVO;
import com.kaizeli.website.response.Result;
import com.kaizeli.website.service.ProjectMemberCardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目成员卡片管理控制器
 * 用于项目成员管理页面的项目卡片列表
 * 统计合同人员表中的项目成员信息
 */
@Slf4j
@RestController
@RequestMapping("/api/project-member-cards")
@RequiredArgsConstructor
@Tag(name = "项目成员卡片管理", description = "项目成员卡片管理相关接口，用于项目成员管理页面")
@SecurityRequirement(name = "Bearer Authentication")
public class ProjectMemberCardController {
    
    private final ProjectMemberCardService projectMemberCardService;
    
    @Operation(summary = "获取项目成员卡片列表", description = "获取按项目编号分组的项目成员卡片列表，包含合同人员统计信息")
    @GetMapping
    public Result<List<ProjectMemberCardVO>> getProjectMemberCards() {
        log.info("获取项目成员卡片列表");
        try {
            List<ProjectMemberCardVO> result = projectMemberCardService.getProjectMemberCards();
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取项目成员卡片列表失败", e);
            return Result.error(500, "获取项目成员卡片列表失败：" + e.getMessage());
        }
    }
    
    @Operation(summary = "根据状态获取项目成员卡片列表", description = "根据是否有已退出成员获取项目成员卡片列表")
    @GetMapping("/status")
    public Result<List<ProjectMemberCardVO>> getProjectMemberCardsByStatus(
            @Parameter(description = "是否有已退出成员", required = false, example = "true")
            @RequestParam(required = false, defaultValue = "false") boolean hasInactiveMembers) {
        log.info("根据状态获取项目成员卡片列表，是否有已退出成员：{}", hasInactiveMembers);
        try {
            List<ProjectMemberCardVO> result = projectMemberCardService.getProjectMemberCardsByStatus(hasInactiveMembers);
            return Result.success(result);
        } catch (Exception e) {
            log.error("根据状态获取项目成员卡片列表失败", e);
            return Result.error(500, "根据状态获取项目成员卡片列表失败：" + e.getMessage());
        }
    }
    
    @Operation(summary = "根据客户ID获取项目成员卡片列表", description = "根据客户ID获取该客户的项目成员卡片列表")
    @GetMapping("/customer/{customerId}")
    public Result<List<ProjectMemberCardVO>> getProjectMemberCardsByCustomer(
            @Parameter(description = "客户ID", required = true, example = "1")
            @PathVariable Long customerId) {
        log.info("根据客户ID获取项目成员卡片列表，客户ID：{}", customerId);
        try {
            List<ProjectMemberCardVO> result = projectMemberCardService.getProjectMemberCardsByCustomer(customerId);
            return Result.success(result);
        } catch (Exception e) {
            log.error("根据客户ID获取项目成员卡片列表失败", e);
            return Result.error(500, "根据客户ID获取项目成员卡片列表失败：" + e.getMessage());
        }
    }
    
    @Operation(summary = "根据签约公司ID获取项目成员卡片列表", description = "根据签约公司ID获取该公司签约的项目成员卡片列表")
    @GetMapping("/contract-company/{contractCompanyId}")
    public Result<List<ProjectMemberCardVO>> getProjectMemberCardsByContractCompany(
            @Parameter(description = "签约公司ID", required = true, example = "1")
            @PathVariable Integer contractCompanyId) {
        log.info("根据签约公司ID获取项目成员卡片列表，签约公司ID：{}", contractCompanyId);
        try {
            List<ProjectMemberCardVO> result = projectMemberCardService.getProjectMemberCardsByContractCompany(contractCompanyId);
            return Result.success(result);
        } catch (Exception e) {
            log.error("根据签约公司ID获取项目成员卡片列表失败", e);
            return Result.error(500, "根据签约公司ID获取项目成员卡片列表失败：" + e.getMessage());
        }
    }
}
